<!-- 台区 -->
<template>
  <div>
    <el-dialog v-bind="dialogProps" :before-close="handleClose" :close-on-click-modal="false"
               :destroy-on-close="true" v-dialogDrag append-to-body>

      <el-form :model="ruleForm" ref="ruleForm" label-width="120px" class="demo-ruleForm" :size="size">
        <el-row :gutter="16" justify="start">
          <!-- <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
              <el-radio-group v-model="radio1" @change="clickRadio" style="margin-bottom:10px">
                  <el-radio-button label="0">新设备</el-radio-button>
                  <el-radio-button label="40">已拆除设备</el-radio-button>
              </el-radio-group>
          </el-col> -->
          <el-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6">
            <el-form-item label="设备名称:" prop="sbmc">
              <el-input v-model="ruleForm.sbmc" clearable placeholder="请选择设备名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6">
            <el-form-item label="出厂编码:" prop="ccbm">
              <el-input v-model="ruleForm.ccbm" clearable placeholder="请选择设备编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6">
            <el-form-item label="运行状态:" prop="yxzt">
              <el-select v-model="ruleForm.yxzt" placeholder="请选择设备状态" clearable style="width: 100%">
                <el-option v-for="item in djztOptions" :key="item.value" :label="item.label"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6">
            <el-form-item style="float:right">
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button style="margin-left: 10px" @click="handleReset">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <el-col :span="11" style="color:#000000">
          列表:
          <div style="height:2px;background:#009688;margin-bottom: 5px;"></div>
          <!-- <vxe-table border stripe height="500" :data="list1.data" @checkbox-all="selectAllEvent"
              @checkbox-change="selectChangeEvent" ref="xTable" :loading="loading"> -->
          <vxe-table border stripe ref="table1" height="400" :column-config="{ resizable: true }" :size="size"
                     :row-config="{ isHover: true }" @radio-change="radioChangeEvent"
                     :radio-config="{ checkMethod: checkRadioMethod, highlight: true }"
                     :checkbox-config="{ highlight: true }"
                     :data="list1.data" :loading="loading">
            <vxe-column type="seq" title="序号" width="60" align="center"></vxe-column>
            <vxe-column type="checkbox" width="60" align="center"></vxe-column>
            <vxe-column field="gldwmc" title="单位" align="center" minWidth="150"></vxe-column>
            <vxe-column field="sbmc" title="设备名称" align="center" minWidth="150"></vxe-column>
            <vxe-column field="ccbm" title="出厂编码" align="center" minWidth="200"></vxe-column>
            <vxe-column field="sblxmc" title="设备类型" align="center" minWidth="150"></vxe-column>
            <vxe-column field="sbgg" title="规格" align="center" minWidth="150"></vxe-column>
            <vxe-column field="xh" title="型号" align="center" minWidth="150"></vxe-column>
            <!-- <vxe-column field="pt" title="PT" align="center" minWidth="150"></vxe-column>
            <vxe-column field="ct" title="CT" align="center" minWidth="150"></vxe-column>
            <vxe-column field="sfjc" title="是否已检测" align="center" minWidth="150">
                <template v-slot="{ row }">
                    <span>{{ row.sex === '1' ? '是' : '否' }}</span>
                </template>
            </vxe-column>
            <vxe-column field="jlfsmc" title="计量方式" align="center" minWidth="150"></vxe-column>
            <vxe-column field="sbztmc" title="设备状态" align="center" minWidth="150"></vxe-column>
            <vxe-column field="cjmc" title="厂家" align="center" minWidth="150"></vxe-column>
            <vxe-column field="ccrq" title="出厂时间" align="center" minWidth="150"></vxe-column> -->

          </vxe-table>
          <vxe-pager perfect size="mini" :current-page="list1.pageNo" :page-size="list1.pageSize"
                     :page-sizes="list1.pageSizes" :total="list1.total"
                     :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
                     @page-change="handlePageChange1">
          </vxe-pager>
        </el-col>
        <el-col :span="2">
          <el-button style="position: absolute;top: 160px;right: 50%;transform:translateX(50%)" type="primary"
                     @click="handleSelect">选择
          </el-button>
          <el-button style="position: absolute;top: 211px;right: 50%;transform:translateX(50%)" type="primary"
                     @click="handleDelete">删除
          </el-button>
          <el-button style="position: absolute;top: 260px;right: 50%;transform:translateX(50%)" type="primary"
                     @click="handleClear">清空
          </el-button>
        </el-col>
        <el-col :span="11" style="color:#000000" :push="2">
          已选列表:
          <div style="height:2px;background:#009688;margin-bottom: 5px;"></div>
          <!-- <vxe-table border stripe height="500" :data="list2.data" @checkbox-all="selectAllEvent1"
              @checkbox-change="selectChangeEvent1" ref="xTable1"> -->
          <vxe-table border stripe ref="table2" height="400" :column-config="{ resizable: true }" :size="size"
                     :row-config="{ isHover: true }" :checkbox-config="{ highlight: true }"
                     :radio-config="{ labelField: 'name' }" :data="list2.data">
            <vxe-column type="seq" title="序号" width="60" align="center"></vxe-column>
            <vxe-column type="checkbox" width="60" align="center"></vxe-column>
            <vxe-column field="gldwmc" title="单位" align="center" minWidth="150"></vxe-column>
            <vxe-column field="sbmc" title="设备名称" align="center" minWidth="150"></vxe-column>
            <vxe-column field="ccbm" title="出厂编码" align="center" minWidth="200"></vxe-column>
            <vxe-column field="sblxmc" title="设备类型" align="center" minWidth="150"></vxe-column>
            <vxe-column field="sbgg" title="规格" align="center" minWidth="150"></vxe-column>
            <vxe-column field="xh" title="型号" align="center" minWidth="150"></vxe-column>
          </vxe-table>
          <vxe-pager perfect size="mini" :current-page="list2.pageNo" :page-size="list2.pageSize"
                     :page-sizes="list2.pageSizes" :total="list2.total"
                     :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
                     @page-change="handlePageChange2">
          </vxe-pager>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
                <el-button @click="handleCancel()" :size="size" icon="fa fa-minus-square-o">取消</el-button>
                <el-button type="primary" @click="handleSubmit()" :size="size"
                           icon="fa fa-save (alias)">确定</el-button>
            </span>
    </el-dialog>
  </div>
</template>
<script>
import {TBizcEneryEquipApi} from '@/api/equip/tBizcEneryEquipApi.js'
import {CommonApi} from '@/api/equip/CommonApi'

export default {
  data() {
    return {
      selectRow: null,
      selectRow1: null,
      size: "medium",
      ruleForm: {
        sbmc: '',
        ccbm: '',
        yxzt: ''
      },
      radio1: '0,40',
      dialogProps: {
        visible: false,
        title: "选择设备",
        width: "80%",
        top: "10px",
      },
      djztOptions: [],
      overheadLine: false,

      list1: {
        data: [],
        pageNo: 1,
        pageSize: 10,
        pageSizes: [10, 20, 50, 100],
        total: 0
      },
      list2: {
        data: [],
        pageNo: 1,
        pageSize: 10,
        pageSizes: [10, 20, 50, 100],
        total: 0
      },
      loading: false,
    };
  },
  methods: {
    showDialog(data) {
      this.dialogProps.visible = true
      this.list2.data = data
      this.list2.total = data.length
      this.fetchdjztOptions()
      this.ruleForm = {
        sbmc: '',
        ccbm: '',
        yxzt: ''
      }
      const pageParams = {
        pageSize: this.list1.pageSize,
        pageNo: this.list1.pageNo,
        entity: {
          sbmc: this.ruleForm.sbmc, //设备名称//
          ccbm: this.ruleForm.ccbm, //规格//
          yxzt: this.ruleForm.yxzt, //运行状态  10-未投运
          sbztbm: this.radio1
        },
        paramsMap: {},
        column: '',
        order: ''
      };
      TBizcEneryEquipApi.page(pageParams).then(res => {
        this.list1 = {
          data: res.data.records,
          pageNo: res.data.current,
          pageSize: res.data.size,
          pageSizes: [10, 20, 50, 100],
          total: res.data.total
        }
      })
    },
    fetchdjztOptions() {
      CommonApi.queryDictData('EN_EQUIP_YXZT').then(res => {
        // if (res.code === '200') {
        let optionList = [
          {label: '请选择', value: ''}
        ]
        res.data.forEach((item) => {
          optionList.push({label: item.value, value: item.code})
        })
        this.djztOptions = optionList
      })
    },
    //查询
    handleSearch() {
      const pageParams = {
        pageSize: this.list1.pageSize,
        pageNo: this.list1.pageNo,
        entity: {
          sbmc: this.ruleForm.sbmc, //设备名称//
          ccbm: this.ruleForm.ccbm, //出厂编码//
          yxzt: this.ruleForm.yxzt, //运行状态  10-未投运
          sbztbm: this.radio1
        },
        paramsMap: {},
        column: '',
        order: ''
      };
      TBizcEneryEquipApi.page(pageParams).then(res => {
        this.loading = false
        this.list1.data = []
        this.list1 = {
          data: res.data.records,
          pageNo: res.data.current,
          pageSize: res.data.size,
          pageSizes: [10, 20, 50, 100],
          total: res.data.total
        }
      })
    },
    //重置
    handleReset() {
      this.ruleForm = {
        sbmc: '',
        ccbm: '',
        yxzt: ''
      }
      const pageParams = {
        pageSize: 10,
        pageNo: 1,
        entity: {
          sbmc: this.ruleForm.sbmc, //设备名称//
          ccbm: this.ruleForm.ccbm, //规格//
          yxzt: this.ruleForm.yxzt, //运行状态  10-未投运
          sbztbm: this.radio1
        },
        paramsMap: {},
        column: '',
        order: ''
      };
      TBizcEneryEquipApi.page(pageParams).then(res => {
        this.list1.data = []
        this.list1 = {
          data: res.data.records,
          pageNo: res.data.current,
          pageSize: res.data.size,
          pageSizes: [10, 20, 50, 100],
          total: res.data.total
        }
      })
    },
    //分页查询
    handlePageChange1({currentPage, pageSize}) {
      this.loading = true
      this.list1.pageNo = currentPage;
      this.list1.pageSize = pageSize;
      this.handleSearch()
    },
    handlePageChange2({currentPage, pageSize}) {
      this.loading = true
      this.list2.pageNo = currentPage;
      this.list2.pageSize = pageSize;
      this.handleSearch()
    },
    //选择列表
    handleSelect() {

      const rowArr = this.$refs.table1.getCheckboxRecords(true)
      rowArr.forEach(d => {
        if (!this.list2.data.find(b => b.ccbm === d.ccbm)) {
          this.list2.data.push(d)
          this.list1.total = this.list1.total - 1
          this.list2.total = this.list2.data.length
        }
      })
      this.list1.data = this.list1.data.filter(d => rowArr.findIndex(b => b.ccbm === d.ccbm) < 0)
    },
    radioChangeEvent({row}) {
      //选中行切换
      this.selectRow = row;
      this.selectRow1 = row;
    },
    //删除已选
    handleDelete() {
      const rowArr = this.$refs.table2.getCheckboxRecords(true);
      this.list2.data = this.list2.data.filter(
        (d) => rowArr.findIndex((b) => b.ccbm === d.ccbm) < 0
      );
      this.list1.total = this.list1.total + rowArr.length
      rowArr.forEach((d) => {
        if (!this.list2.data.find((b) => b.ccbm === d.ccbm)) {
          this.list1.data.unshift(d);
          this.list2.total -= 1
        }
      });
    },
    //清空已选
    handleClear() {
      this.list2.data = [];
      this.list2.total = 0
      this.handleSearch()
    },
    checkRadioMethod({row}) {
      console.log(row, 'ppppp');
      //设置能够被选中的记录
      return
    },
    //确定
    handleSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$emit("onSelectStations", this.list2.data);
          this.dialogProps.visible = false;
        } else {
          return false;
        }
      });

    },
    //取消
    handleCancel() {
      this.$refs.ruleForm.resetFields()
      this.dialogProps.visible = false;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.handleCancel()
          this.dialogProps.visible = false;
        })
        .catch((_) => {
        });
    },
  },
};
// 创建一个简单的超链接渲染
</script>
<style>
.el-dialog__footer {
  width: 100%;
  border-top: 1px solid #e8e8e8;
}
</style>
